package com.by.service.impl;

import com.by.bean.EnterpriseCategory;
import com.by.common.crud.BaseServiceImpl;
import com.by.mapper.EnterpriseCategoryMapper;
import com.by.service.EnterpriseCategoryService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EnterpriseCategoryServiceImpl extends BaseServiceImpl<EnterpriseCategory, EnterpriseCategoryMapper> implements EnterpriseCategoryService {

    @Override
    public int save(EnterpriseCategory entity) {
        //根据父类别id查询最大子类的编码
        String maxCode = baseMapper.queryMaxCodeByParentId(entity.getParentId());
        if (null == maxCode) {
            if (entity.getParentId() == 0) {
                //增加的一级类别
                entity.setCode("101");
            } else {
                //非一级类别  根据父类别id查询对应编码
                String tmp = baseMapper.queryCode(entity.getParentId());
                entity.setCode(tmp + "01");
            }
        } else {
            //存在 将编码转换为数字
            int tmp = Integer.parseInt(maxCode);
            ++tmp;
            entity.setCode(String.valueOf(tmp));
        }
        return super.save(entity);
    }

    @Override
    public List tree() {
        return baseMapper.tree(0);
    }
}
